package vip.xiaomoli.courier.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextHelper implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextHelper.applicationContext = applicationContext;
    }

    public static <T> T getBean(String name) {
        if (null != applicationContext) {
            //noinspection unchecked
            return (T) applicationContext.getBean(name);
        }
        return null;
    }

    public static <T> T getBean(Class<T> clazz) {
        if (null != applicationContext) return applicationContext.getBean(clazz);
        return null;
    }

    public static String[] getBeanNamesForType(Class<?> clazz) {
        if (null != applicationContext) return applicationContext.getBeanNamesForType(clazz);
        return null;
    }

}
